//
//  MyActivityKaQuanViewController.m
//  FD
//
//  Created by Yasin on 2018/10/18.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//
//活动推荐 @“活动推荐”
#import "MyActivityRecommendKaQuanViewController.h"
//view
#import "MyKaQuanCell.h"
#import "FDNowDataView.h" //无数据占位图

//model
#import "MyActivityRecomendModel.h"

//controller
#import "WaiSongViewController.h" //外送点餐vc
#import "CouponDetaiController.h"


#define kActiveRecomentNoDataViewFrame CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 100)
@interface MyActivityRecommendKaQuanViewController ()<UITableViewDelegate,UITableViewDataSource>
/**tableVieq*/
@property (nonatomic,  strong) UITableView *tv;

/**数据源*/
@property (nonatomic,  strong) NSMutableArray *dataArr;
/**无数据占位图*/
@property (nonatomic, strong) FDNowDataView *nodataView;

@end

@implementation MyActivityRecommendKaQuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI]; //UI
    
    [self setupRefresh];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self loadCouponListData]; //加载数据
}

#pragma mark - 下拉刷新功能
- (void)setupRefresh {
    FDRefreshHader *refreshHeader = [FDRefreshHader headerWithRefreshingTarget:self refreshingAction:@selector(loadCouponListData)];
    self.tv.mj_header = refreshHeader;
    [self.tv.mj_header beginRefreshing];
}


- (void)setupUI {
    CGRect tempFrame = self.view.bounds;
    tempFrame.origin.y = FD_NavH + 40;
    tempFrame.size.height = SCREEN_HEIGHT - FD_NavH-40-FD_TabbarSafeBottomMargin;
    self.tv.frame = tempFrame;
    [self.view addSubview:self.tv];
    //设置上padding 40
//    self.tv.contentInset = UIEdgeInsetsMake(40, 0, 0, 0);
    self.tv.tableFooterView = [UIView new];

    //无数据占位图片
    [self.tv addSubview:self.nodataView];
    //默认隐藏
    self.nodataView.hidden = YES;
}


#pragma mark - getter

- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        
        //代理
        self.tv.delegate = self;
        self.tv.dataSource = self;
        [self.tv registerClass:[MyKaQuanCell class] forCellReuseIdentifier:@"MyKaQuanCell"];
        _tv.separatorStyle = UITableViewCellSelectionStyleNone;
    }
    return _tv;
}
- (FDNowDataView *)nodataView {
    if (!_nodataView) {
        _nodataView = [[FDNowDataView alloc] initWithFrame:kActiveRecomentNoDataViewFrame];
    }
    return _nodataView;
}


- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyKaQuanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyKaQuanCell"];
    
    //取出对应的model
    MyActivityRecomendModel *model = self.dataArr[indexPath.row];
    cell.activityRecomendModel = model;

    FDWeakSelf;
    //点击事件：领取优惠券
    cell.myKaQuanCellClickWaiSongDianCanBlock = ^{
        FDLog(@"领取额优惠券");
        
        [weakSelf requestReceiveCouponWithCouponId:model.couponId];
    };
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;

//    MyCouponModel *model = self.dataArr[indexPath.row];
//    return model.cellHeight; //默认:145
}

#pragma mark - 二 请求数据
/**请求优惠券详情列表数据*/
- (void)loadCouponListData {
    [FDHttpAPi requestSYCouponListSuccessBlock:^(id responseObjc) {
        [self.tv.mj_header endRefreshing];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:@"SUCCESS"]) {
            NSArray * dataArr = responseObjc[@"data"];
            
            [self.dataArr removeAllObjects];//清空
//            if (!dataArr.count) return ;
            
            for (NSDictionary *dic in dataArr) {
                MyActivityRecomendModel *model = [MyActivityRecomendModel mj_objectWithKeyValues:dic];
                [self.dataArr addObject:model];
            }
            
            //刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                if (self.dataArr.count) {
                    self.nodataView.hidden = YES;
                }else {
                    self.nodataView.hidden = NO;
                }
                
                [self.tv reloadData];
            });
            
            
        }else {
            [JRToast showWithText:@"describe"];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"发生了错误:%@",[error localizedDescription]);
        [self.tv.mj_header endRefreshing]; //结束刷新
    }];
    
}

//领取优惠券
- (void)requestReceiveCouponWithCouponId:(NSNumber *)couponId {

    [FDHttpAPi requestObtainCouponWithId:couponId successBlock:^(id responseObjc) {
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            
            [self loadCouponListData]; //刷新数据
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"领取优惠券失败:%@",[error localizedDescription]);
    }];
}

@end
